//
//  DKBankCardPhoneAlertView.m
//  DanKU
//
//  Created by 蓝鳍互娱 on 2023/12/13.
//

#import "DKBankCardPhoneAlertView.h"
#import "DKLoginAccountView.h"
#import "DKLoginMsgCodeView.h"
#import "DKHomeHttp.h"
@interface DKBankCardPhoneAlertView()
@property (nonatomic, weak)zhPopupController * dk_AuthBackAlertViewPop;
@property (nonatomic,strong) DKLoginAccountView *dk_LoginAccountView;
@property (nonatomic,strong) DKLoginMsgCodeView *dk_LoginMsgCodeView;
@property (nonatomic,strong) UILabel *dk_LoginAlertLab;
@property (nonatomic,strong) UIButton *dk_BankCardAlertSubimBtn;

@property (nonatomic, strong)NSDictionary * dataDic;
@property (nonatomic, copy)void(^SureScuessBankCardAlertBlock)(void);
@end
@implementation DKBankCardPhoneAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.whiteColor;
        [self dk_addSubView];
        [self requestUserBaseInfo];
    }
    return self;
}
- (void)dk_addSubView{
    WeakSelf(self)
    UILabel * dk_BankCardAlertHeadLab = [[UILabel alloc] init];
    dk_BankCardAlertHeadLab.textColor = DK_Hex(@"#333333");
    dk_BankCardAlertHeadLab.font = DK_FONT_Medium(15);
    dk_BankCardAlertHeadLab.text = @"Enter verification code to confirm";
    [self addSubview:dk_BankCardAlertHeadLab];
    [dk_BankCardAlertHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(40)));
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(16)));
    }];
    //close
    UIButton * dk_LoginCloseBtn = [[UIButton alloc] init];
    [dk_LoginCloseBtn setBackgroundImage:DK_GetImage(@"dk_LoginAgreementClose") forState:UIControlStateNormal];
    [dk_LoginCloseBtn addTarget:self action:@selector(goCancle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_LoginCloseBtn];
    [dk_LoginCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
        make.width.equalTo(@(DK_IsWidthScale(17)));
    }];
    //账号
    self.dk_LoginAccountView = [[DKLoginAccountView alloc] init];
    self.dk_LoginAccountView.smsType = @"modify_bank";
    self.dk_LoginAccountView.dk_LoginAccountTextField.enabled = NO;
    [self addSubview:self.dk_LoginAccountView];
    [self.dk_LoginAccountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(30)));
        make.right.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_BankCardAlertHeadLab.mas_bottom).offset(DK_IsWidthScale(30));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    //验证码
    self.dk_LoginMsgCodeView = [[DKLoginMsgCodeView alloc] init];
    [self addSubview:self.dk_LoginMsgCodeView];
    [self.dk_LoginMsgCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(30)));
        make.right.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_LoginAccountView.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_LoginMsgCodeView.goCheckMsgCodeBlock = ^(BOOL isPhoneCodeNull) {
        weakSelf.dk_BankCardAlertSubimBtn.enabled = !isPhoneCodeNull;
        if(!isPhoneCodeNull){
            weakSelf.dk_LoginAlertLab.hidden = YES;
            [weakSelf.dk_LoginAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(weakSelf.dk_LoginMsgCodeView.mas_bottom).offset(DK_IsWidthScale(0));
                make.height.equalTo(@(DK_IsWidthScale(0)));
            }];
        }
    };
    //提示
    self.dk_LoginAlertLab = [[UILabel alloc] init];
    self.dk_LoginAlertLab.textColor = DK_Hex(@"#FC7E08");
    self.dk_LoginAlertLab.font = DK_FONT_Regular(10);
    self.dk_LoginAlertLab.hidden = YES;
    self.dk_LoginAlertLab.text = @"*Wrong verification code";
    [self
     addSubview:self.dk_LoginAlertLab];
    [self.dk_LoginAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(56)));
        make.right.equalTo(@(-DK_IsWidthScale(56)));
        make.top.equalTo(self.dk_LoginMsgCodeView.mas_bottom).offset(DK_IsWidthScale(0));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.dk_BankCardAlertSubimBtn = [[UIButton alloc] init];
    [self.dk_BankCardAlertSubimBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [self.dk_BankCardAlertSubimBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.dk_BankCardAlertSubimBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_BankCardAlertSubimBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_BankCardAlertSubimBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_BankCardAlertSubimBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.dk_BankCardAlertSubimBtn.enabled = NO;
    [self.dk_BankCardAlertSubimBtn addTarget:self action:@selector(submitBankCardSms) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dk_BankCardAlertSubimBtn];
    [self.dk_BankCardAlertSubimBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_LoginAlertLab.mas_bottom).offset(DK_IsWidthScale(30));
    }];
}
- (void)requestUserBaseInfo{
    WeakSelf(self)
    //获取用户信息
    [DKHomeHttp apiGetUserBaseInfoForAPI:@{} complete:^{
        weakSelf.dk_LoginAccountView.dk_LoginAccountTextField.text = [DKGlobalHelper defaultManager].userBaseInfoMdl.phone;
    }];
}
- (void)goCancle{
    [_dk_AuthBackAlertViewPop dismiss];
}
- (void)submitBankCardSms{
    WeakSelf(self)
    NSMutableDictionary *userInfoDataDic = [[NSMutableDictionary alloc] init];
    [userInfoDataDic setObject:[NSString dicChangeString:self.dataDic] forKey:@"data"];
    [userInfoDataDic setObject:@"BANK_INFO" forKey:@"step"];
    [userInfoDataDic setObject:[self.dk_LoginMsgCodeView getLoginCode] forKey:@"vcode"];
    [DKPublicHttp apiModifyBankCardForAPI:userInfoDataDic complete:^{
        [weakSelf.dk_AuthBackAlertViewPop dismiss];
        weakSelf.SureScuessBankCardAlertBlock();
    } fail:^{
        weakSelf.dk_LoginAlertLab.hidden = NO;
        [weakSelf.dk_LoginAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.dk_LoginMsgCodeView.mas_bottom).offset(DK_IsWidthScale(10));
            make.height.equalTo(@(DK_IsWidthScale(14)));
        }];
    }];
}
- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self makeCornerOnTopleftRight:DK_IsWidthScale(50)];
}
+ (void)dk_ShowBankCardPhoneAlertView:(NSDictionary *)dataDic withView:(UIView *)keyView complete:(void(^)(void))complete{
    DKBankCardPhoneAlertView * dk_BankCardPhoneAlertView = [[DKBankCardPhoneAlertView alloc] init];
    zhPopupController * sureAuthScuessCtrl = [[zhPopupController alloc] initWithView:dk_BankCardPhoneAlertView size:CGSizeMake(DK_SCREEN_W, DK_IsWidthScale(356)+DK_SafeAreaBottom)];
    sureAuthScuessCtrl.presentationStyle = zhPopupSlideStyleFromBottom;
    sureAuthScuessCtrl.layoutType = zhPopupLayoutTypeBottom;
    sureAuthScuessCtrl.presentationTransformScale = 0.56;
    sureAuthScuessCtrl.dismissonTransformScale = 0.56;
    sureAuthScuessCtrl.dismissOnMaskTouched = NO;
    dk_BankCardPhoneAlertView.dataDic = dataDic;
    dk_BankCardPhoneAlertView.dk_AuthBackAlertViewPop = sureAuthScuessCtrl;
    dk_BankCardPhoneAlertView.SureScuessBankCardAlertBlock = complete;
    [sureAuthScuessCtrl showInView:keyView completion:^{
    }];
}
@end
